home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Sep / di9809cd / TVDEMO1.pas < prev    next >
Pascal/Delphi Source File  |  1997-12-12  |  6KB  |  233 lines

  1. unit TVDEMO1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls, TelevisionLIB, SharedMemory;
  8.  
  9. type
  10.   TTelevisionDemo = class(TForm)
  11.     TVOffImage: TImage;
  12.     TVOnImage: TImage;
  13.     Panel1: TPanel;
  14.     PowerBtn: TButton;
  15.     ChBtn1: TButton;
  16.     ChBtn2: TButton;
  17.     ChBtn3: TButton;
  18.     ChBtn4: TButton;
  19.     ChBtn5: TButton;
  20.     ChBtn6: TButton;
  21.     ChBtn7: TButton;
  22.     ChBtn8: TButton;
  23.     ChBtn9: TButton;
  24.     ChBtn0: TButton;
  25.     SleepBtn: TButton;
  26.     CCBtn: TButton;
  27.     ChannelLbl: TLabel;
  28.     SleepLbl: TLabel;
  29.     TVTimer: TTimer;
  30.     TileMap: TImage;
  31.     CCLbl: TLabel;
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure PowerBtnClick(Sender: TObject);
  34.     procedure TVTimerTimer(Sender: TObject);
  35.     procedure SleepBtnClick(Sender: TObject);
  36.     procedure ChBtn0Click(Sender: TObject);
  37.     procedure FormPaint(Sender: TObject);
  38.     procedure CCBtnClick(Sender: TObject);
  39.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  40.   private
  41.     { Private declarations }
  42.     SharedMemory : PTELEVISION_SMEM;
  43.     SharedMem : TSharedMem;
  44.     procedure UpdateText;
  45.     procedure TileBackground;
  46.   public
  47.     { Public declarations }
  48.     Tile : TBitmap;
  49.     TVPower : Boolean;
  50.     TVCC : Boolean;
  51.     TVChannel : Integer;
  52.     BuildChannel : string;
  53.     TVCCWords : Integer;
  54.     TVSleepTimer : LongInt;
  55.     TVSleepActive : Boolean;
  56.     TVElaspedTime : LongInt;
  57.     TVBaseTime : LongInt;
  58.   end;
  59.  
  60. var
  61.   TelevisionDemo: TTelevisionDemo;
  62.  
  63. implementation
  64.  
  65. {$R *.DFM}
  66.  
  67. //
  68. //  Utility function(s)
  69. //
  70. procedure TTelevisionDemo.TileBackground;
  71. var num_across, num_down, yoff, xoff, y_coord, x_coord, y, x : integer;
  72. begin
  73.   //tiling logic based upon code by Casey Charlton
  74.   num_across := (Width div Tile.Width) + 1;
  75.   num_down := (Height div Tile.Height) +1;
  76.   xoff := (Width - ((num_across + 1) * Tile.Width)) div 2;
  77.   yoff := (Height - ((num_down + 1) * Tile.Height)) div 2;
  78.   for x := 0 to num_across do
  79.     for y := 0 to num_down do
  80.     begin
  81.       x_coord := (x * Tile.Width) + xoff;
  82.       y_coord := (y * Tile.Height) + yoff;
  83.       Canvas.Draw(x_coord, y_coord, Tile);
  84.     end;
  85. end;
  86.  
  87. //
  88. // Form/System event-related procedures
  89. //
  90. procedure TTelevisionDemo.FormCreate(Sender: TObject);
  91. begin
  92.   SharedMem:=nil;
  93.   Tile:=TBitmap.Create;
  94.   Tile.Assign(TileMap.Picture);
  95.   SleepLbl.Caption:='';
  96.   ChannelLbl.Caption:='';
  97.   CCLbl.Caption:='';
  98.   TVPower:=False;
  99.   TVOffImage.Visible:=True;
  100.   TVOnImage.Visible:=False;
  101.   TVCC:=False;
  102.   TVChannel:=1;
  103.   BuildChannel:='';
  104.   TVCCWords:=0;
  105.   TVSleepTimer:=0;
  106.   TVBaseTime:=0;
  107.   TVSleepActive:=False;
  108.   TVElaspedTime:=0;
  109.   //create shared memory communications
  110.   SharedMem:=TSharedMem.Create('Television_SMEM',SizeOf(TTELEVISION_SMEM));
  111.   SharedMemory:=SharedMem.Buffer;
  112. end;
  113.  
  114. procedure TTelevisionDemo.FormPaint(Sender: TObject);
  115. begin
  116.    TileBackground;
  117. end;
  118.  
  119. procedure TTelevisionDemo.FormClose(Sender: TObject; var Action: TCloseAction);
  120. begin
  121.   //shutdown shared memory communications
  122.   SharedMem.Free;
  123. end;
  124.  
  125. //
  126. //  Master timing mechanism to update shared memory and process
  127. //    internal timing counters
  128. //
  129. procedure TTelevisionDemo.TVTimerTimer(Sender: TObject);
  130. begin
  131.   if TVPower then
  132.   begin
  133.     if TVBaseTime<>0 then TVElaspedTime:=Round((GetTickCount-TVBaseTime)/1000);
  134.     if TVSleepActive then Dec(TVSleepTimer);
  135.     if TVCC then Inc(TVCCWords,Round(Random(30)));
  136.   end;
  137.   UpdateText;
  138.   if (SharedMem<>nil) then
  139.   begin
  140.     if TVPower then SharedMemory^.Power:=1 else SharedMemory^.Power:=0;
  141.     SharedMemory^.Channel:=TVChannel;
  142.     if TVCC then
  143.       SharedMemory^.ClosedCaptioningEnabled:=1
  144.     else
  145.       SharedMemory^.ClosedCaptioningEnabled:=0;
  146.     SharedMemory^.SleepTimer:=TVSleepTimer;
  147.     SharedMemory^.TimeOn:=TVElaspedTime;
  148.     SharedMemory^.ClosedCaptioningWordsPerSecond:=TVCCWords;
  149.   end;
  150. end;
  151.  
  152. //
  153. //  Handle screen updates
  154. //
  155. procedure TTelevisionDemo.UpdateText;
  156. begin
  157.   if TVPower then
  158.   begin
  159.     if (TVSleepTimer=0) then
  160.       if TVSleepActive then
  161.       begin
  162.         TVPower:=False;
  163.         SleepLbl.Caption:='';
  164.         ChannelLbl.Caption:='';
  165.         CCLbl.Caption:='';
  166.         TVOnImage.Visible:=False;
  167.         TVOffImage.Visible:=True;
  168.       end else
  169.         SleepLbl.Caption:=''
  170.     else
  171.       SleepLbl.Caption:='Sleep ('+IntToStr(TVSleepTimer)+')';
  172.     if TVChannel<10 then
  173.       ChannelLbl.Caption:='C0'+IntToStr(TVChannel)
  174.     else
  175.       ChannelLbl.Caption:='C'+IntToStr(TVChannel);
  176.     if TVCC then
  177.       CCLbl.Caption:='CC On'
  178.     else
  179.       CCLbl.Caption:='';
  180.   end else
  181.   begin
  182.     SleepLbl.Caption:='';
  183.     ChannelLbl.Caption:='';
  184.     CCLbl.Caption:='';
  185.   end;
  186. end;
  187.  
  188. //
  189. //  User control-related event handlers
  190. //
  191. procedure TTelevisionDemo.PowerBtnClick(Sender: TObject);
  192. begin
  193.   TVPower:=not TVPower;
  194.   TVOnImage.Visible:=Not TVOnImage.Visible;
  195.   TVOffImage.Visible:=Not TVOffImage.Visible;
  196.   TVCCWords:=0;
  197.   TVSleepTimer:=0;
  198.   TVElaspedTime:=0;
  199.   TVBaseTime:=GetTickCount;
  200.   TVSleepActive:=False;
  201.   UpdateText;
  202. end;
  203.  
  204. procedure TTelevisionDemo.SleepBtnClick(Sender: TObject);
  205. begin
  206.   TVSleepActive:=not TVSleepActive;
  207.   if TVSleepActive then TVSleepTimer:=60 else TVSleepTimer:=0;
  208.   UpdateText;
  209. end;
  210.  
  211. procedure TTelevisionDemo.ChBtn0Click(Sender: TObject);
  212. begin
  213.   if not TVPower then Exit;
  214.   if BuildChannel='' then
  215.     BuildChannel:=Trim(TButton(Sender).Caption)
  216.   else
  217.   begin
  218.     BuildChannel:=BuildChannel+Trim(TButton(Sender).Caption);
  219.     TVChannel:=StrToInt(BuildChannel);
  220.     BuildChannel:='';
  221.     UpdateText;
  222.   end;
  223. end;
  224.  
  225. procedure TTelevisionDemo.CCBtnClick(Sender: TObject);
  226. begin
  227.   TVCC:=not TVCC;
  228.   TVCCWords:=0;
  229.   UpdateText;
  230. end;
  231.  
  232. end.
  233.